/*
 *   Author:
 *   vyouzhi <vyouzhi@163.com>
 *   http://www.xda.cn
 *
 *   File:                    proc_data_lib.c
 *   Create Date:      2011-08-11 10:51:22
 */

#include <string.h>
#include <stdio.h>

#include "xml_lib.h"
#include "sdb_struct.h"
#include "mysql_lib.h"
#include "memcache_lib.h"
#include "proc_data_lib.h"
#include "mem_lib.h"
/*#include "record_lib.h"*/
#include "time_lib.h"
/*#include "convey_lib.h"*/
/*
long setData(const char *sql){
	long lastid, upid;
	char *table_n;
	MYSQL *sconn;

	if ((sconn = connect_sql()) == NULL) return(-1);
	lastid = set_mysql(sql, sconn);

	if(-1 == lastid) return (-1);

	table_n = get_logname();
	upid = dorecord(sql, table_n);
	doconvey(upid, table_n);
	
	free(table_n);

	return lastid;	
}

char *getid(long id){
	char idxml[1024]; 
	char *sd;

	bzero(idxml, 1024);
	sprintf(idxml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><Row0><id>%ld</id></Row0></data>", id);
	sd = strdup(idxml);

	return sd;
}
*/
char *select_mysql(char *sql, int type){
	char *dosql = sql;
	char *buff_xml;
	Sdb *sdb;
	MYSQL *sm_conn;
	
	sm_conn = connect_sql();
	if(sm_conn == NULL) return NULL;

	sdb = get_mysql(dosql, type, sm_conn);

	if(sdb == NULL){
		printf("data is null\n");
		return NULL;
	}else{
		buff_xml = set_xml(sdb);
		Sdb_destroy(sdb);
	}

	return buff_xml;
}

char *getData(recv_xml *rxml){
	char *val, *key;
	int i = 0, extime = 0;
	recv_xml *gdxml = rxml;

	val = NULL;
	key = gdxml->key;
	extime = gdxml->extime;

	if(key != NULL && 0 != strcmp(key, "null") && extime > 0){
		val = get_memcache(gdxml->key);
		i = 1;
	}
	if(val != NULL) return val;
	
	val = select_mysql(gdxml->sub, gdxml->type);

	if(i == 1 && val != NULL){
		set_memcache(val, gdxml->key, extime, strlen(val));
	}

	return val;
}

void FreeXml(recv_xml *fxml){
	SQLFree(fxml->rw);
	SQLFree(fxml->sub);
	SQLFree(fxml->key);
	SQLFree(fxml->host);
	SQLFree(fxml->action);
	SQLFree(fxml);	
}

char *SwitchData(char *s_d){
	recv_xml *rxml;
	int sd_len, lastid;
	char *senddata;
	
	senddata = NULL;

	sd_len = strlen(s_d);	
	rxml = get_xml(s_d, sd_len);

	/*printf("sub %s\n", rxml->sub);*/
	if(rxml == NULL) return NULL;

	if(rxml->rw == NULL){
		FreeXml(rxml);
		return NULL;
	}

	if(rxml->sub != NULL && strcmp(rxml->rw, "w")==0){
		
		lastid = setData(rxml->sub);	
		if(lastid > 0 && rxml->returnid == 1) senddata = getid(lastid);

	}else if(rxml->sub != NULL && !strcmp(rxml->rw, "r") && strlen(rxml->sub) > 0){	

		senddata = getData(rxml);
	}else if(!strcmp(rxml->rw, "u")){
		printf("recv client---\n");
		/*getupdate(rxml->lastid);*/
		printf("here---\n");
		/*recode = 1;
		senddata = NULL; */
	}else if(!strcmp(rxml->rw, "g")){
		printf("g server---\n");
		/*updateing(rxml->lastid, rxml->host, rxml->port);
		*senddata = NULL;				*/
	}else if(!strcmp(rxml->rw, "i")){
		printf("insert log and insert table\n");
		/*insert_log(rxml->sub, rxml->id, rxml->logname);*/
	}else if(rxml->action != NULL && !strcmp(rxml->rw, "m") && !strcmp(rxml->action, "del")){
		/*del_mem(rxml->key);*/
	}

	FreeXml(rxml);
	return senddata;
}


/* vim: set ts=4 sw=4: */

